package com.hqw.mindanews;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import android.app.Activity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.BmobUser;
import cn.bmob.v3.listener.FindListener;
import cn.bmob.v3.listener.SaveListener;

import com.hqw.mindanews.adapter.CommentAdapter;
import com.hqw.mindanews.application.SysApplication;
import com.hqw.mindanews.bean.Comments;
import com.hqw.mindanews.bean.NewsMsg;
import com.hqw.mindanews.bean.User;
import com.hqw.mindanews.view.RoundAngleImageView;
import com.hqw.mindanews.view.XListView;

public class NewsDetailActivity extends Activity implements OnClickListener {
	private NewsMsg mNewsMsg;
	private TextView mUserName;
	private ImageView mUserIcon;
	private TextView mContent;
	private TextView mTime;
	private TextView mTitle;
	private TextView mChannel;
	private ImageView mImageView1;
	private ImageView mImageView2;
	private ImageView mImageView3;
	private ImageView mImageView4;
	private ImageView mImageView5;
	private ImageView mImageView6;
	private CommentAdapter commentAdapter;
	private XListView mXListView;
	private List<Comments> mCommentsList;
	private Comments mComments;
	private EditText mEditComment;
	private Button mSendComment;
	private RoundAngleImageView mTopHead;
	private TextView mCancelEdit;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.news_detail_layout);
		// 获取传过来newsMsg对象
		mNewsMsg = (NewsMsg) getIntent().getSerializableExtra("newsMsg");
		mComments = new Comments();
		initView();
		SysApplication.getInstance().addActivity(this);
	}

	private void initView() {
		mTopHead = (RoundAngleImageView) findViewById(R.id.top_head);
		mTopHead.setVisibility(View.GONE);
		mCancelEdit = (TextView) findViewById(R.id.cancel_edit);
		mCancelEdit.setVisibility(View.VISIBLE);
		mCancelEdit.setText("《返回");
		mTitle = (TextView) findViewById(R.id.title);
		 mTitle.setText("新闻正文");
		mCancelEdit.setOnClickListener(this);
		mXListView = (com.hqw.mindanews.view.XListView) findViewById(R.id.pull_to_refresh_commons);
		mXListView.setPullRefreshEnable(false);// 禁止下拉刷新
		mXListView.setPullLoadEnable(true);// 是否允许加载更多
		mCommentsList = new ArrayList<Comments>();
		// 给xlistView添加头 即新闻正文
		LayoutInflater infla = getLayoutInflater();// LayoutInflater.from(this);
		View headerView = infla.inflate(R.layout.news_detail_header_layout,
				null);

		mUserIcon = (ImageView) headerView.findViewById(R.id.user_icon_detail);
		mTime = (TextView) headerView.findViewById(R.id.created_time_text_view);
		mUserName = (TextView) headerView.findViewById(R.id.username_text_view);
		mChannel = (TextView) headerView.findViewById(R.id.channel_text_view);
		mContent = (TextView) headerView.findViewById(R.id.content_text_view);
		mImageView1 = (ImageView) headerView
				.findViewById(R.id.detail_image_one);
		mImageView2 = (ImageView) headerView
				.findViewById(R.id.detail_image_two);
		mImageView3 = (ImageView) headerView
				.findViewById(R.id.detail_image_three);
		mImageView4 = (ImageView) headerView
				.findViewById(R.id.detail_image_four);
		mImageView5 = (ImageView) headerView
				.findViewById(R.id.detail_image_five);
		mImageView6 = (ImageView) headerView
				.findViewById(R.id.detail_image_six);
		mEditComment = (EditText) findViewById(R.id.edit_comment);
		mSendComment = (Button) findViewById(R.id.send_comment);
		mSendComment.setOnClickListener(this);
		mXListView.addHeaderView(headerView, null, true);
		mXListView.setAdapter(null);
		mXListView.setHeaderDividersEnabled(false);

		showViewFromMsg();
		getCommentData();
	}

	private void getCommentData() {
		BmobQuery<Comments> query = new BmobQuery<Comments>();
		query.include("user");
		query.addWhereEqualTo("newsMsg", mNewsMsg);
		query.order("createdAt");
		query.findObjects(this, new FindListener<Comments>() {

			@Override
			public void onSuccess(List<Comments> comments) {
				mCommentsList = comments;
				commentAdapter = new CommentAdapter(NewsDetailActivity.this,
						mCommentsList);
				mXListView.setAdapter(commentAdapter);
			}

			@Override
			public void onError(int arg0, String arg1) {
				// TODO Auto-generated method stub

			}
		});

	}

	private void showViewFromMsg() {
		switch (mNewsMsg.getChannel()) {
		case 0:
			mChannel.setText("校园");
			break;
		case 1:
			mChannel.setText("社团");
			break;
		case 2:
			mChannel.setText("活动");
			break;
		case 3:
			mChannel.setText("通知");
			break;
		case 4:
			mChannel.setText("兼职");
			break;
		case 5:
			mChannel.setText("二手");
			break;

		default:
			break;
		}
		mContent.setText(mNewsMsg.getContent());
		mTime.setText(mNewsMsg.getCreatedAt());
		mNewsMsg.getUser().getBmobFile()
				.loadImage(NewsDetailActivity.this, mUserIcon);
		mUserName.setText(mNewsMsg.getUser().getUsername());

		int numberOfImage = mNewsMsg.getBmobFiles().size();
		if (numberOfImage > 0) {
			for (int i = 0; i < numberOfImage; i++) {
				switch (i) {
				case 0:
					mImageView1.setVisibility(View.VISIBLE);
					mNewsMsg.getBmobFiles().get(0).loadImage(this, mImageView1);
					break;
				case 1:
					mImageView2.setVisibility(View.VISIBLE);
					mNewsMsg.getBmobFiles().get(1).loadImage(this, mImageView2);
					break;
				case 2:
					mImageView3.setVisibility(View.VISIBLE);
					mNewsMsg.getBmobFiles().get(2).loadImage(this, mImageView3);
					break;
				case 3:
					mImageView4.setVisibility(View.VISIBLE);
					mNewsMsg.getBmobFiles().get(3).loadImage(this, mImageView4);
					break;
				case 4:
					mImageView5.setVisibility(View.VISIBLE);
					mNewsMsg.getBmobFiles().get(4).loadImage(this, mImageView5);
					break;
				case 5:
					mImageView6.setVisibility(View.VISIBLE);
					mNewsMsg.getBmobFiles().get(5).loadImage(this, mImageView6);
					break;
				default:
					break;
				}
			}
		}

	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.send_comment:
			User currentUser = BmobUser.getCurrentUser(NewsDetailActivity.this,
					User.class);
			String comment = mEditComment.getText().toString();
			mComments.setUser(currentUser);
			mComments.setComment(comment);
			Date dt = new Date();
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日  HH:mm:ss");
			String time = sdf.format(dt);
			mComments.setCommentTime(time);
			mComments.setNewsMsg(mNewsMsg);
			mCommentsList.add(mComments);
			mEditComment.setText("");
			mComments.save(this, new SaveListener() {

				@Override
				public void onSuccess() {
					commentAdapter.notifyDataSetChanged();
				}

				@Override
				public void onFailure(int arg0, String arg1) {

				}
			});
			break;
			case R.id.cancel_edit:
				finish();
				break;
		default:
			break;
		}

		
	}

}
